package net.chathu.airgw.packets.chat 
{
	import net.chathu.airgw.Config;
	import net.chathu.airgw.model.User;
	import net.chathu.airgw.packets.gw.serverpackets.EchoPacket;
	import net.chathu.airgw.packets.gw.serverpackets.JoinPacket;
	import net.chathu.airgw.packets.gw.serverpackets.NickModePacket;
	import net.chathu.airgw.PrefixManager;
	import net.chathu.airgw.util.StringUtil;

	/**
	 * ...
	 * @author ze
	 */
	public class IAddUsers extends ChatPacket
	{
		
		public function IAddUsers() 
		{
			
		}
		
		override public function execute():void
		{
			
			var id:int;
			var regName:String;
			var sex:int;
			var regId:int;
			var color:String;
			var disturb:int;
			var age:int;
			var type:int;
			
			var index:int = 2;

			try {

				id=parseInt(data[index]);
				disturb = parseInt(data[index + 3]);
				regId=parseInt(data[index+5]);
				sex = parseInt(data[index + 9]);
				type = parseInt(data[index + 7]);
				color = data[index + 13];
				age=data[index+15];
				regName = StringUtil.convertFromISO88591(data[index + 17]);
				
				var isItYou:Boolean = (regName == sessionData.chatData.username);
				var user:User = new User(id, regId, regName, sex, age, color, disturb, type);
				sessionData.chatData.users.add(user,isItYou);
				
				//itt nem kell konvertalas..
				if (regName == sessionData.chatData.username)
				{
					sessionData.chatData.userId = id;
				}
				
				
				if (sessionData.chatData.isInUsersRoom)
				{
					//TODO: mute, voice kezeles!
					sendToClient(new JoinPacket(user.userHost, Config.USERSLIST_CHANNEL));
					var mode:String = PrefixManager.getUserModes(sessionData, user, null);
					if (mode.length > 0) {
						sendToClient(new NickModePacket(Config.USERSLIST_CHANNEL, user.name, "+" + mode));
					}
				}

			}catch(err:Error) {
				trace("IAddUsers error:" + err.name);
				throw err;
			}

		}
		
	}

}